package net.game.server.util.crypto;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.*;

public class RSAKeyGen {
    private static PublicKey publicKey;
    private static PrivateKey privateKey;
    
    public static void generate(File pub, File priv) {
        try {
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("rsa");
            KeyPair kp = kpg.generateKeyPair();
            publicKey = kp.getPublic();
            privateKey = kp.getPrivate();
            FileOutputStream publ = new FileOutputStream(pub);
            FileOutputStream priva = new FileOutputStream(priv);
            publ.write(publicKey.getEncoded());
            priva.write(privateKey.getEncoded());
            publ.close();
            priva.close();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static PublicKey getPublicKey() {
        return publicKey;
    }
    
    public static PrivateKey getPrivateKey() {
        return privateKey;
    }
}
